import {
    fetchIdsByType, fetchWeather
} from '../api'

export default {
    // ensure data for rendering given list type
    FETCH_LIST_DATA: ({
        commit,
        dispatch,
        state
    }, {
        type,
        index,
        action
    }) => {
        commit('SET_ACTIVE_TYPE', {
            type
        });
        // commit(action === 'next' ? 'ADD_TYPE_RANK_INDEX' : 'DEL_TYPE_RANK_INDEX', {type, index});
        return fetchIdsByType(type, index)
            .then(data => {
                switch (action) {
                    case 'next':
                        {
                            commit('ADD_TYPE_RANK_INDEX', {
                                type,
                                index
                            });
                            break;
                        }
                    case 'prev':
                        {
                            commit('DEL_TYPE_RANK_INDEX', {
                                type
                            });
                            break;
                        }
                    default:
                        {
                            commit('INIT_TYPE_RANK_INDEX', {
                                type
                            });
                        }
                }
                commit('SET_LIST', {
                    type,
                    data
                })

            })
    },
    FETCH_WEATHER:({
        commit,
        dispatch,
        state
    })=> {
        console.log(999)
        return fetchWeather().then(cityName=>{
            console.log(888)
            commit('SET_WEATHER', cityName)
        }).catch((err)=>{
            console.log(err)
        })
    }
}
